package com.finger.gulimall.product.app;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.finger.common.utils.PageUtils;
import com.finger.common.utils.R;
import com.finger.gulimall.product.entity.CategoryEntity;
import com.finger.gulimall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;


/**
 * 商品三级分类
 *
 * @author finger
 * @email 909128695@qq.com
 * @date 2022-03-16 13:38:29
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 删除商品菜单Redis缓存
     * @return
     */
    @PutMapping("/updateRedisCache")
    public R updateRedisCache(){
        categoryService.updateRedisCache();
        return R.ok();
    }

    /**
     * 查找种类的树形菜单
     *
     * @return 种类的树形菜单
     */
    @RequestMapping(method = RequestMethod.GET, value = "listForTree")
    public R listForTree() {

        // 查找商品种类的树形菜单
        List<CategoryEntity> entities = categoryService.listForTree();

        return R.ok().put("data", entities);
    }

    @RequestMapping(method = RequestMethod.GET, value = "listWithTree")
    public R listWithTree() {

        // 查找商品种类的树形菜单
        List<CategoryEntity> entities = categoryService.listWithTree();

        return R.ok().put("data", entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params) {

        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    // @RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Integer catId) {
        CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:category:save")
    public R save(@Valid @RequestBody CategoryEntity category, BindingResult result) {

        if (result.hasErrors()) {

            HashMap<String, Object> map = new HashMap<>();
            result.getFieldErrors().forEach(item -> {
                map.put(item.getField(), item.getDefaultMessage());
            });

            return R.error(200, "您提交的数据不合法").put("data", map);

        }

        categoryService.saveCategory(category);

        return R.ok();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateDetails(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Integer[] catIds) {
        // categoryService.removeByIds(Arrays.asList(catIds));
        // 自定义根据菜单Id集合批量删除菜单
        categoryService.removeBachIds(Arrays.asList(catIds));

        return R.ok();
    }

    /**
     * 根据菜单Id批量更新菜单信息
     *
     * @param categoryEntities 菜单数组
     * @return R
     */
    @RequestMapping("/updateBatch")
    public R updateBatch(@RequestBody CategoryEntity[] categoryEntities) {

        categoryService.updateBatchById(Arrays.asList(categoryEntities));

        return R.ok();
    }

}
